package cn.leon.content.service.impl;

import cn.leon.common.pojo.EasyUITreeNode;
import cn.leon.common.pojo.TaotaoResult;
import cn.leon.content.service.ContentCategoryService;
import cn.leon.mapper.Content_categoryMapper;
import cn.leon.pojo.Content_category;
import cn.leon.pojo.Content_categoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

    @Autowired
    private Content_categoryMapper content_categoryMapper;

    @Override
    public List<EasyUITreeNode> getContentCategory(long parentId) {
        //设置查询条件
        Content_categoryExample example = new Content_categoryExample();
        Content_categoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //得到List<TbContentCategory>
        List<Content_category> content_categories = content_categoryMapper.selectByExample(example);
        List<EasyUITreeNode> easyUITreeNodes = new ArrayList<>();
        for (Content_category content_category : content_categories) {
            EasyUITreeNode easyUITreeNode = new EasyUITreeNode();
            easyUITreeNode.setId(content_category.getId());
            easyUITreeNode.setText(content_category.getName());
            easyUITreeNode.setState(content_category.getIsParent()?"closed":"open");
            easyUITreeNodes.add(easyUITreeNode);
        }
        return easyUITreeNodes;
    }

    @Override
    public TaotaoResult addContentCategory(long parentId, String name) {
        // 1、接收两个参数：parentId、name
        // 2、向tb_content_category表中插入数据。
        // a)创建一个ContentCategory对象
        Content_category content_category = new Content_category();
        //补全对象属性
        content_category.setIsParent(false);
        content_category.setParentId(parentId);
        content_category.setName(name);
        //排列序号，表示同级类目的展现次序，如数值相等则按名称次序排列。取值范围:大于零的整数
        content_category.setSortOrder(1);
        //状态。可选值:1(正常),2(删除)
        content_category.setStatus(1);
        content_category.setCreated(new Date());
        content_category.setUpdated(new Date());
        //插入数据库
        content_categoryMapper.insert(content_category);
        // 3、判断父节点的isparent是否为true，不是true需要改为true。
        Content_category parentNode= content_categoryMapper.selectByPrimaryKey(parentId);
        if(!parentNode.getIsParent()){
            parentNode.setIsParent(true);
            //更新父节点
            content_categoryMapper.updateByPrimaryKey(parentNode);
        }
        // 4、需要主键返回。
        // 5、返回TaotaoResult，其中包装TbContentCategory对象
        return TaotaoResult.ok(content_category);
    }

    @Override
    public TaotaoResult updateContentCategory(long id, String name) {
        Content_category content_category = content_categoryMapper.selectByPrimaryKey(id);
        content_category.setName(name);
        content_categoryMapper.updateByPrimaryKey(content_category);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteContentCategory(long id) {
       deleteCon(id);
       return TaotaoResult.ok();
    }
    //通过id得到list
    public List<Content_category> getList(long id){
        Content_categoryExample content_categoryExample = new Content_categoryExample();
        Content_categoryExample.Criteria criteria = content_categoryExample.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<Content_category> content_categoryList = content_categoryMapper.selectByExample(content_categoryExample);
        return content_categoryList;
    }
    //递归删除
    public void deleteCon(long id){
        //先判断是否为父节点
        Content_category content_category = content_categoryMapper.selectByPrimaryKey(id);
        long parentId = content_category.getParentId();
        if(!content_category.getIsParent()){
            //不为父
            content_categoryMapper.deleteByPrimaryKey(id);
            updateParent(parentId);
        }else{
            for (Content_category contentCategory : getList(id)) {
                deleteCon(contentCategory.getId());
            }
            content_categoryMapper.deleteByPrimaryKey(id);
        }
    }
    //修改该节点对象为子
    public void updateParent(long parentId){
        List<Content_category> list = getList(parentId);
        if (list.size() == 0){
            Content_category parentNode = content_categoryMapper.selectByPrimaryKey(parentId);
            parentNode.setIsParent(false);
            content_categoryMapper.updateByPrimaryKey(parentNode);
        }
    }
}
